package com.david.dubbo.router;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Router;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author David
 * @version V1.0
 * @Package com.david.dubbo.router
 * @date 2021/4/15 23:30
 */
public class RestartingInstanceRouter implements Router {

    private final URL url;
    private final ReadyRestartInstances instances;

    public RestartingInstanceRouter(URL url) {
        this.url = url;
        instances=ReadyRestartInstances.create();
    }

    public URL getUrl() {
        return url;
    }

    public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
        //此处过滤掉还在启动的服务节点，通过服务名称和ip来过滤
        return invokers.stream().filter(
                i->!instances.hasRestartingInstance(i.getUrl().getParameter("remote.application"),i.getUrl().getIp()))
                .collect(Collectors.toList());
    }

    public boolean isRuntime() {
        return false;
    }

    public boolean isForce() {
        return false;
    }

    public int getPriority() {
        return 0;
    }
}
